/**
 * WanlShop状态管理器 - 社区问答模块
 * @ version 1.0.0
 **/
 
 
let lifeData = {};

try {
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData');
} catch (e) {

}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = [
	'vuex_user',
	'vuex_token',
	'vuex_third',
	'vuex_isAdmin',
	'vuex_othen_user',
	'vuex_invitecode',
	'vuex_signin',
	'vuex_web',
	'vuex_theme',
	'vuex_config',
	'vuex__token__'
];
 
 
// 保存变量到本地存储中
const saveLifeData = function(key, value) {
	// 判断变量名是否在需要存储的数组中
	if (saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmpLifeData = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmpLifeData = tmpLifeData ? tmpLifeData : {}, tmpLifeData[key] = value
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmpLifeData);
	}
}
 
export default {
	namespaced: true,
	state: {
		// 如果上面从本地获取的lifeData对象下有对应的属性，就赋值给state中对应的变量
		// 加上vuex_前缀，是防止变量名冲突，也让人一目了然
		vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {},
		vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
		vuex_isAdmin: lifeData.vuex_isAdmin ? lifeData.vuex_isAdmin : false,
		vuex_delete_article: false,
		vuex_delete_question: false,
		vuex_version: '1.0.1',
		vuex_config: lifeData.vuex_config ? lifeData.vuex_config : {},
		vuex_theme: lifeData.vuex_theme ? lifeData.vuex_theme : {},
		vuex_bio: '这家伙很懒，什么也没写！',
		vuex_othen_user: lifeData.vuex_othen_user ? lifeData.vuex_othen_user : '',
		vuex_signin: lifeData.vuex_signin ? lifeData.vuex_signin : {},
		vuex_invitecode: lifeData.vuex_invitecode ? lifeData.vuex_invitecode : '', //邀请码	
		vuex_wx_uid: '',
		vuex__token__: '',
		vuex_web: lifeData.vuex_web ? lifeData.vuex_web : 0,
		vuex_third: {},
		vuex_parse_style: {
			// 字符串的形式
			h1: 'padding:20rpx 0;',
			h2: 'padding:10rpx 0;',
			h3: 'padding:10rpx 0;',
			h4: 'padding:10rpx 0;',
			h5: 'padding:5rpx 0;',
			h6: 'padding:5rpx 0;',
			hr: 'background: #e6e6e6;border: none;display: block;height: 1px;margin-bottom: 30rpx;margin-top: 30rpx;',
			ul: 'margin-bottom:20rpx;padding-left:30rpx;',
			ol: 'margin-bottom:20rpx;padding-left:30rpx;',
			code: "background-color: #f6f6f6;margin: 0 5rpx;padding: 6rpx 8rpx;border-radius: 6rpx;text-align:center;font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, 'Microsoft Yahei', Arial, sans-serif;",
			pre: 'white-space: pre;overflow: auto;background: #f6f6f6;border-radius: 8rpx;border: none;color: #1a1a1a;margin-bottom: 20rpx;padding:20rpx;',
			'pre code': 'margin:0;padding:0;color:#6c6c6c',
			blockquote: 'padding: 15rpx;margin:0 0 20rpx 0;border-radius: 6rpx;',
			p: 'margin-bottom:20rpx',
			table: 'width:100%;margin-bottom:20rpx;border-collapse: collapse;',
			th: 'background-color: whitesmoke;border: 1px solid #e6e6e6;padding:10rpx;',
			td: 'border: 1px solid #e6e6e6;padding:10rpx;'
		},
		vuex_tabbar: {
			color: "#999999",
			selectColor: "#000000",
			bgColor: "#FFFFFF",
			borderStyle: "white",
			list: [{
					path: "pages/community/question/home",
					image: "static/img/tabbar/question.png",
					selectedImage: "static/img/tabbar/question-hl.png",
					text: "问题"
				},
				{
					path: "pages/community/article/index",
					image: "static/img/tabbar/article.png",
					selectedImage: "static/img/tabbar/article-hl.png",
					text: "文章"
				},
				{
					path: "pages/community/zone/index",
					image: "static/img/tabbar/zone.png",
					selectedImage: "static/img/tabbar/zone-hl.png",
					text: "专区"
				},
				{
					path: "pages/community/experts/index",
					image: "static/img/tabbar/experts.png",
					selectedImage: "static/img/tabbar/experts-hl.png",
					text: "专家"
				},
				{
					path: "pages/community/my/index",
					image: "static/img/tabbar/my.png",
					selectedImage: "static/img/tabbar/my-hl.png",
					text: "我的"
				}
			]
		},
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
	
		setInfo(state, payload) {
			state = Object.assign(state,payload);
		},
		setVuexToken(state, payload) {
			uni.setStorageSync("vuex__token__", payload);
			state.vuex__token__ = payload
		},
	},
	actions: {
		async login({state, commit, dispatch, rootState}, data) {
			commit('setUserInfo', data.userinfo);
			state.isLogin = true; // 登录状态强制 开启
			// 根据notice.vue 而知，fastadmin没有此三项默认字段，手动添加，想同步修改该这两处即可
			state.pushs = true; // 推送
			state.voice = true; // 提示音
			state.shock = true; // 震动
			uni.setStorageSync("wanlshop:user", state);
			// 统计信息
			dispatch('statistics', data.statistics)
		},
		setToken({state, commit, dispatch, rootState}, payload) {
			console.log(payload)
			uni.setStorageSync("token", payload);
			state.vuex_token = payload
		},
		async statistics({state, dispatch, rootState}, payload){
			let states = rootState.statistics;
			for (let i in payload) {
				for (let j in states) {
					if (i === j) {
						states[j] = payload[i];
					}
				}
			}
			uni.setStorageSync("wanlshop:statis", states);
		}
	}
};